package datesource;

import homework.jdbc.dao.JDBCUtils;
import homework.jdbc.domain.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by Administrator on 2017/9/14.
 */
public class UserDao {
    public int pageSize=1;
public List<User>findPage(int page) throws SQLException {
    Connection conn = C3p0Utils.getConnection();
    PreparedStatement ps =null;
    List<User> list=new ArrayList<>();
    try {
        String sql = "SELECT * FROM student ORDER BY id DESC LIMIT ?,?";//预编译sql
        ps = conn.prepareStatement(sql);                                  //预处理
        ps.setInt(1,(page-1)*pageSize);
        ps.setInt(2,pageSize);
        ResultSet set = ps.executeQuery(sql);                    //执行结果放入set

        while (set.next()) {
            User user = new User(set.getInt(1),
                                 set.getString(2),
                                 set.getString(3));
            list.add(user);
        }
        return list;

    } catch (SQLException e) {
        e.printStackTrace();
    }

return null;
}

}
